/*
 * Angry Monsters, Windows Mobile 7 XNA Game FW demo
 * Tero Paananen, tepaanan@gmail.com
 * 2011 Finland
*/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;

namespace WP7Box2d
{

    interface ButtonListenerIF
    {
        void ButtonPressed(Button.ButtonType buttonType);
    }
    
    
    class Button
    {
        public enum ButtonType
        {
            ECancelBtn = 1,
            ECreateBtn = 2,
            EEditBtn = 3,
            ELeftBtn = 4,
            ERightBtn = 5,
            ESaveBtn = 6,
            EPlayBtn = 7,
            EExitBtn = 8
        }

        private Random m_random = new Random();

        private Texture2D m_btnTexture;
        private Rectangle m_rectangle;
        private Rectangle m_originalRectangle;
        private double m_gameTime;
        private bool isBtnDown;
        private ButtonType m_btnType;

        private ButtonListenerIF m_listener;
        
        public Button(ButtonListenerIF listener, ButtonType type, Texture2D btnTexture)
        {
            m_listener = listener;
            m_btnType = type;
            m_btnTexture = btnTexture;
            isBtnDown = false;
            m_gameTime = 0;
        }

        public void SetPosition(Rectangle p)
        {
            m_rectangle = p;
        }

        public bool handleTouch(Vector2 touchPos)
        {
            if (m_rectangle.Contains(new Microsoft.Xna.Framework.Point((int)touchPos.X, (int)touchPos.Y)))
            {
                m_originalRectangle = m_rectangle; 
                isBtnDown = true;
                return true;
            }
            return false;
        }


        public void Draw(SpriteBatch spriteBatch, GameTime gameTime)
        {
            if (isBtnDown)
            {
                // Keep button pressed
                m_gameTime += gameTime.ElapsedGameTime.TotalSeconds;
                if (m_gameTime > 0.9)
                {
                    isBtnDown = false;
                    m_gameTime = 0;
                    m_listener.ButtonPressed(m_btnType);
                }
                else if (m_gameTime > 0.6)
                {
                    m_rectangle = m_originalRectangle;
                }
                else if (m_gameTime < 0.6)
                {
                    m_rectangle.X = m_random.Next(m_rectangle.X - 4, m_rectangle.X + 4);
                    m_rectangle.Y = m_random.Next(m_rectangle.Y - 4, m_rectangle.Y + 4);
                }
                spriteBatch.Draw(m_btnTexture, m_rectangle, Color.White);
            }
            else
            {
                spriteBatch.Draw(m_btnTexture, m_rectangle, Color.White);
            }
        }
    }
}
